package pers.cz.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * @program: LuoThinking-cloud
 * @description: 用户信息工具
 * @author: Cheng Zhi
 * @create: 2022-06-19 12:46
 **/
@Component
public class UserUtils {

    public Integer getCurrentUserId() {
        return Integer.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
    }


    /**
     * 获取用户名称
     * @return
     */
    public String getCurrentUserName() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        return username;
    }
}
